// subPages/mine/consultList/consultList.js
const app = getApp()
var util =  require('../../../utils/util.js');
/**
 * type
 * 1 健康咨询后台
 * 
 */
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.type){
      that.setData({
        type: options.type,
        isType: true
      })
      util.setNavBarTitle(that.getTitle(that.data.type))
    }
  },

  //获取标题
  getTitle:function(type){
    var title = ''
    switch (type) {
      case '1':
        title = '健康咨询后台'
        break;
      case '2':
        title = '最新消息'
        break;
      case '3':
        title = '我的提问'
        break;
    }
    return title
  },

  //专家端获取数据
  getData:function(typeUrl,type){
    var that = this
    var status = ''
    if(type == '2'){
      status = '0'
    }
    util.getAjax(`${app.globalData.apiUrl}${typeUrl}`,function(res){
      console.log(res)
      if(res.data.length != 0){
        that.setData({
          isData: true,
          consultList: res.data
        })
      }
    },{expert_id: app.globalData.identity.id,status: status})
  },

  //用户端获取数据
  getData2:function(typeUrl){
    var that = this
    util.getAjax(`${app.globalData.apiUrl}${typeUrl}`,function(res){
      console.log(res)
      if(res.data.length != 0){
        that.setData({
          isData: true,
          consultList: res.data
        })
      }
    },{user_id: that.data.userId})
  },

  //最新消息
  newMessage:function(){
    if(this.data.num == 0){
      util.showModal('提示','当前无最新消息')
      return
    }
    wx.navigateTo({
      url: '../consultList/consultList?type=' + 2
    })
  },

  //咨询窗口
  consultBtn:function(e){
    var that = this
    let item = e.currentTarget.dataset.item;
    let type = ''
    if(that.data.type == '1' || that.data.type == '2'){
      //专家端
      type = 3
    }else if(that.data.type == '3'){
      //用户端
      type = 2
    }
    // console.log(item)
    wx.navigateTo({
      url: '/subPages/health/consultHistory/consultHistory?type=' + type + '&item=' + JSON.stringify(item)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if(!that.data.isType) return
    var typeUrl = ''
    if(that.data.type == '1' || that.data.type == '2'){
      //专家
      that.setData({
        num: app.globalData.identity.num?app.globalData.identity.num: 0
      })
      if(app.globalData.identity.id){
        typeUrl = '/expert_get_communtiy_health_expert_tall_order'
        that.getData(typeUrl,that.data.type)
      }
    }else if(that.data.type == 3){
      //用户
      typeUrl = '/user_get_communtiy_health_expert_tall_order'
      if(app.globalData.userId){
        that.setData({
          userId: app.globalData.userId
        })
        that.getData2(typeUrl)
      }else{
        app.userIdCallback = userId => {
          if (userId != '') {
            that.setData({
              userId: userId
            });
            that.getData2(typeUrl)
          }
        }
      }
    }
    wx.stopPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if(!that.data.isType) return
    var typeUrl = ''
    if(that.data.type == '1' || that.data.type == '2'){
      //专家
      that.setData({
        num: app.globalData.identity.num?app.globalData.identity.num: 0
      })
      if(app.globalData.identity.id){
        typeUrl = '/expert_get_communtiy_health_expert_tall_order'
        that.getData(typeUrl,that.data.type)
      }
    }else if(that.data.type == 3){
      //用户
      typeUrl = '/user_get_communtiy_health_expert_tall_order'
      if(that.data.userId){
        that.getData2(typeUrl)
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})